
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;
using System.Text;

namespace ScrewTurn.Wiki {

	public partial class Operation : BasePage {

		string op = "";

		protected void Page_Load(object sender, EventArgs e) {
			Page.Title = Resources.Messages.OperationTitle + " - " + Settings.WikiTitle;

			if(Request["Operation"] == null) Response.Redirect(Settings.DefaultPage + Settings.PageExtension);

			op = Request["Operation"].ToLowerInvariant();

			switch(op) {
				case "deletemessage":
					Page.Title = "Delete Message - " + Settings.WikiTitle;
					mlwOperation.ActiveViewIndex = 0;
					PrepareDeleteMessage();
					break;
			}

		}

		#region Operation: DeleteMessage

		private void PrepareDeleteMessage() {
			if(SessionFacade.LoginKey == null) {
				// Login
				Response.Redirect("/Members/Login.aspx?Redirect=Operation.aspx" + Tools.UrlEncode("?Operation=DeleteMessage&Message=" + Request["Message"] + "&Page=" + Request["Page"]));
			}
			else if(!SessionFacade.Admin) {
				// Access Denied
				Response.Redirect("AccessDenied.aspx");
			}

			if(Page.IsPostBack) return;

			string ms = Request["Message"];
			string pg = Request["Page"];
			if(ms == null || ms.Length == 0 || pg == null || pg.Length == 0) Response.Redirect(Settings.DefaultPage + Settings.PageExtension);

			PageInfo page = Pages.Instance.FindPage(pg);
			if(page == null) Response.Redirect(Settings.DefaultPage + Settings.PageExtension);
			if(page.Provider.ReadOnly) Response.Redirect(page.Name + Settings.PageExtension);

			int id = -1;
			try {
				id = int.Parse(ms);
			}
			catch {
				Response.Redirect(Settings.DefaultPage + Settings.PageExtension);
			}

			Message message = Pages.Instance.FindMessage(new List<Message>(Pages.Instance.GetPageMessages(page)), id);
			if(message == null) Response.Redirect(Settings.DefaultPage + Settings.PageExtension);

			StringBuilder sb = new StringBuilder();
			sb.Append("<b>");
			sb.Append(message.Subject);
			sb.Append("</b><br /><small>");
			sb.Append(Resources.Messages.Posted);
			sb.Append(" ");
			string shift = "";
			if(Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
			sb.Append(Tools.AlignWithPreferences(message.DateTime, shift).ToString(Settings.DateTimeFormat));
			sb.Append(" ");
			sb.Append(Resources.Messages.By);
			sb.Append(" ");
			sb.Append(Users.Instance.UserLink(message.Username));
			sb.Append("</small><br /><br />");
			sb.Append(FormattingPipeline.FormatWithPhase3(FormattingPipeline.FormatWithPhase1And2(message.Body, null), null));

			lblDeleteMessageContent.Text = sb.ToString();
		}

		protected void btnDeleteMessage_Click(object sender, EventArgs e) {
			int id = int.Parse(Request["Message"]);
			PageInfo page = Pages.Instance.FindPage(Request["Page"]);
			Log.LogEntry("Message deletion requested for " + page.Name + "." + id.ToString(), EntryType.General, SessionFacade.Username);
			bool done = Pages.Instance.RemoveMessage(page, id, chkDeleteMessageReplies.Checked);
			Response.Redirect(Request["Page"] + Settings.PageExtension + "?Discuss=1");
		}

		protected void btnCancelDeleteMessage_Click(object sender, EventArgs e) {
			Response.Redirect(Request["Page"] + Settings.PageExtension + "?Discuss=1");
		}

		#endregion

	}

}
